//
//  uiTextureManager.cpp
//  trail-map-editor
//
//  Created by Giorgio Tino on 2/19/13.
//
//

#include "ui.h"

using namespace tinyxml2;
using namespace std;

uiTextureManager* uiTextureManager::instance = 0;

uiTextureManager::~uiTextureManager()
{
    atlases.clear();
}

void uiTextureManager::init()
{
    // Load all the atlases
    XMLDocument doc;
	doc.LoadFile("texture-atlas-names.xml"); // TBD : hardcoded? really?
    
    // Read the root element
    const XMLElement* root = doc.RootElement();
    
    // Parse all the atlas names and load them
    const XMLElement* textureAtlasName = root->FirstChildElement();
    
    while(textureAtlasName)
    {
        const char* name = textureAtlasName->Attribute("name");
        
        uiTextureAtlas* atlas = new uiTextureAtlas(name);
        
        // Add atlas to the map
        atlases[name] = atlas;
        
        textureAtlasName = textureAtlasName->NextSiblingElement();
    }
}

uiTexture* uiTextureManager::getTexture(const char* name) const
{
    uiTextureMap::const_iterator it = textures.find(name);
    
    return it !=textures.end() ? it->second : NULL;
}

bool uiTextureManager::addTexture(const char* name, uiTexture* texture)
{
    textures[name] = texture;
}